﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Obj;
using BLL;

public partial class AreaRestrita_Automovel : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ValidarUsuario();
        if (!IsPostBack)
        {
            List<objAutomovel> lista = new List<objAutomovel>();
            BLLAutomovel regra = new BLLAutomovel();
            lista = regra.lstAutomovel();
            gdvAutomovel.DataSource = lista;
            gdvAutomovel.DataBind();

            ddlGaragem.DataTextField = "Nome";
            ddlGaragem.DataValueField = "GaragemID";
            BLLGaragem regraGaragem = new BLLGaragem();
            ddlGaragem.DataSource = regraGaragem.lstGaragens();
            ddlGaragem.DataBind();
        }
        
    }
    protected void btnNovo_Click(object sender, EventArgs e)
    {
        Response.Redirect("AutomovelForm.aspx", true);
    }
    protected void gdvAutomovel_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            objAutomovel Automovel = (objAutomovel)e.Row.DataItem;
            ImageButton imgEditar = (ImageButton)e.Row.FindControl("imgEditar");
            imgEditar.CommandArgument = Automovel.AutomovelID.ToString();

            ImageButton imgExcluir = (ImageButton)e.Row.FindControl("imgExcluir");
            imgExcluir.CommandArgument = Automovel.AutomovelID.ToString();

            Label lblPreco = (Label)e.Row.FindControl("lblPreco");
            lblPreco.Text = Automovel.Preco.ToString();
        }
    }
    protected void btnFiltrar_Click(object sender, EventArgs e)
    {
        objAutomovel automovel = new objAutomovel();
        BLLAutomovel Regra = new BLLAutomovel();
        if (ddlGaragem.SelectedValue != String.Empty)
        {
            List<objAutomovel> Lista = Regra.lstAutomovelByGaragem(Convert.ToInt32(ddlGaragem.SelectedValue));

            gdvAutomovel.DataSource = Lista;
            gdvAutomovel.DataBind();
        }
    }
    protected void gdvAutomovel_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Excluir")
        {
            BLLAutomovel regra = new BLLAutomovel();
            objAutomovel automovel = new objAutomovel(Convert.ToInt32(e.CommandArgument));
            regra.Save(automovel, action.DELETE);

            ScriptManager.RegisterClientScriptBlock(
            this,
            typeof(Page),
            "ToggleScript",
            "alert('Automóvel excluído com sucesso!');window.location.href='Automovel.aspx'",
            true);
        }
        else if (e.CommandName == "Editar")
        {
            Session["AutomovelID"] = e.CommandArgument;
            Response.Redirect("AutomovelForm.aspx", true);
        }
    }
}